//
//  TopApiConvert.m
//  

//
//  Created by yuike6 yuike6 on 12-2-27.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "TopApiConvert.h"
#import "header.h"
//#import "ymall.h"


@interface TopApiConvert()
{
    BOOL bSetIsMobile;
}
@end

@implementation TopApiConvert

@synthesize nick = nick_;
@synthesize numIids = num_iids_;

@synthesize pid = pid_;


@synthesize isMobile;

- (void) dealloc
{
    YRelease(nick_);    
    YRelease(num_iids_);
    
    YRelease(pid_);
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self) {
        self.apiContent = TOP_API_TAOKE_CONVERT_API_TEMPLATE;    
        bSetIsMobile = FALSE;
    }
    
    return self;
}

- (NSString *) sing
{
    NSArray * apiBody = [self.apiContent split:@"?"];
    if (!apiBody || [apiBody count] < 2) {
        return nil;
    }
    
    NSString * body = [apiBody objectAtIndex:1];
    
    NSArray * keyValues = [body split:@"&"];
    keyValues = [keyValues sortedArrayUsingSelector:@selector(compare:)];
    
    NSString * sing = [[[NSString alloc] initWithString:self.appSecret] autorelease];
    for (NSString * keyValue in keyValues)
    {
        if ([NSString isNilOrEmpty:keyValue]) 
        {
            continue;
        }                
        
        if ([keyValue isIncludeSubString:@"sign="]) {
            continue;
        }
        
        
        
        sing = [sing stringByAppendingString:[keyValue replace:@"=" with:@""]];        
    }
    sing = [sing stringByAppendingString:self.appSecret];    
    
    sing = [sing md5Digest];
    sing = [sing upper];    
    return sing;
}

- (NSString *) topApiUrl
{
 
    self.apiContent = [self.apiContent replace:_PARAM_TOP_TIMESTAMP_ with:self.time];
    self.apiContent = [self.apiContent replace:_PARAM_TOP_APPKEY_ with:self.appKey];
    self.apiContent = [self.apiContent replace:_PARAM_TOP_METHOD_ with:TOP_API_TAOKE_CONVERT];
    
    self.apiContent =[self.apiContent replace:_PARAM_TOP_NICK_ with:self.nick];
    
    NSString * products = [NSString stringWithFormat:@""];
    int index = 1;
    for (NSString * iid in self.numIids) {
        if ([NSString isNilOrEmpty:iid]) {
            continue;
        }

        products = [products stringByAppendingString:iid];
        if (index < [self.numIids count]) {
            products = [products stringByAppendingString:@","];
        }
        index++;
    }
    
    YkSystem * system = [[[YkSystem alloc] init] autorelease];   

    self.apiContent = [self.apiContent replace:@"%%imei%%" with:[system deviceImei]];
    self.apiContent = [self.apiContent replace:@"%%imsi%%" with:[system deviceImsi]];  

    
    self.apiContent =[self.apiContent replace:_PARAM_TOP_PRODUCTIDS_ with:products];    
    
    self.apiContent = [self.apiContent replace:_PARAM_TOP_SIGN_ with:[self sing] ];     
    
    return self.apiContent;
}




-(void) setIsMobile:(BOOL)isMobileValue
{
    if (bSetIsMobile) 
    {
        return;
    }
    
    NSString * str = nil;
    if (isMobileValue) {
        str = [NSString stringWithString:@"true"];
    }
    else
    {
        str = [NSString stringWithString:@"false"];
    }
    self.apiContent = [self.apiContent replace:_PARAM_TOP_IS_MOBILE_ with:str];
    
    bSetIsMobile = TRUE;
}


@end
